import {permissionList}  from '@/config/PermissionConfig'
import { ref, onMounted, useTemplateRef } from 'vue';
import {storeToRefs} from 'pinia'
import {type R} from "@/types/R"
import {request} from "@/utils/Request"
import {ElMessage, ElMessageBox} from "element-plus"
import {type PageInfo} from "@/types/PageInfo"
import {randomId} from "@/utils/IdUtils"
import { useApVersionStore } from './ApVersionStore';
import type { AppVersionVO, SelectNowVersionVO } from './ApVersionTypes';
import { useApplicationStore } from '@/pages/application/applicationStore'

export const useApVersionHooks = function(){

    let apVersionStore = useApVersionStore()
    let {
        willUploadAppVersionDialog,
        uploadAppVersionPlatform,
        uploadAppVersionMethod,
        uploadAppVersionIsForce,
        uploadAppVersionInfo,
        uploadAppVersionNumber,
        uploadAppVersionFileUrl,
        uploadAppVersionWgtUrl,

        appVersionListAO,

        updateAppVersion,
        willUpdateAppVersionDialog,

        showQrCodeDialog,
        showQrCode,
        showQrCodeVersion,
    } = storeToRefs(apVersionStore)

    let applicationStore = useApplicationStore();
    let {isCollapse} = storeToRefs(applicationStore)

    const willUploadAppVersion = ()=>{
        willUploadAppVersionDialog.value = true
        uploadAppVersionPlatform.value = "Android"
    }

    const uploadAppVersionCancel = ()=>{
        willUploadAppVersionDialog.value = false
    }

    const uploadAppVersionSave = async()=>{
        let r:R<any> = await request.post("/appVersion/insertVersion",{
            platform: uploadAppVersionPlatform.value,
            versionStr: uploadAppVersionNumber.value,
            info: uploadAppVersionInfo.value,
            fileUrl: uploadAppVersionFileUrl.value,
            wgtUrl: uploadAppVersionWgtUrl.value,
            updateMethod: uploadAppVersionMethod.value,
            isForce: uploadAppVersionIsForce.value
        })
        if(r.code == "success"){
            ElMessage.success("上传成功");
            willUploadAppVersionDialog.value = false
            select()
            selectVersion()
        }
    }

    let pageInfo = ref<PageInfo<AppVersionVO>>()

    onMounted(()=>{
        selectVersion()
        select()
    })

    let selectNowVersionLoading = ref<boolean>(false)
    let selectNowVersionVO = ref<SelectNowVersionVO>()
    const selectVersion = async()=>{
        selectNowVersionLoading.value = true
        let r:R<SelectNowVersionVO> = await request.post("/appVersion/selectNowVersion")
        if(r.code == "success"){
            selectNowVersionVO.value = r.data
        }
        selectNowVersionLoading.value = false
    }

    let selectLoading = ref<boolean>(false)

    const select = async()=>{
        selectLoading.value = true
        let r:R<PageInfo<AppVersionVO>> = await request.post("/appVersion/appVersionList",appVersionListAO.value)
        if(r.code == "success"){
            pageInfo.value = r.data
        }
        selectLoading.value = false

    }

    const deleteAppVersion = (id:string)=>{
        ElMessageBox.confirm('此操作将永久删除该版本, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async()=>{
            let r:R<any> = await request.post("/appVersion/deleteVersion",{
                id: id,
            })
            if(r.code == "success"){
                ElMessage.success("删除成功")
                select()
                selectVersion()
            }
        })
    }

    const releaseAppVersion = (row:any)=>{
        ElMessageBox.confirm('此操作将发布该版本, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async()=>{
            let r:R<any> = await request.post("/appVersion/releaseVersion",{
                id: row.id,
            })
            if(r.code == "success"){
                select()
                selectVersion()
            }
        })
    }

    const callbackAppVersion = (row:any)=>{
        ElMessageBox.confirm('此操作将回调该版本, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async()=>{
            let r:R<any> = await request.post("/appVersion/callbackVersion",{
                id: row.id,
            })
            if(r.code== "success"){
                select()
                selectVersion()
            }
        })
    }

    const willUpdateAppVersion = (row:any)=>{
        updateAppVersion.value = {
            id: row.id,
            platform: row.platform,
            versionStr: row.versionStr,
            releaseTime: row.releaseTime,
            info: row.info,
            fileUrl: row.fileUrl,
            status: row.status,
            wgtUrl: row.wgtUrl,
            updateMethod: row.updateMethod,
            isForce: row.isForce
        }
        willUpdateAppVersionDialog.value = true
    }
    const updateAppVersionCancel = ()=>{
        willUpdateAppVersionDialog.value = false
    }
    const updateAppVersionSave = async()=>{
        let r:R<any> = await request.post("/appVersion/updateVersion",{
            id: updateAppVersion.value?.id,
            platform: updateAppVersion.value?.platform,
            info: updateAppVersion.value?.info,
            fileUrl: updateAppVersion.value?.fileUrl,
            wgtUrl: updateAppVersion.value?.wgtUrl,
            updateMethod: updateAppVersion.value?.updateMethod,
            isForce: updateAppVersion.value?.isForce
        })
        if(r.code == "success"){
            ElMessage.success("修改成功");
            willUpdateAppVersionDialog.value = false
            select()
            selectVersion()
        }
    }

    return {
        willUploadAppVersion,
        willUploadAppVersionDialog,
        uploadAppVersionPlatform,
        uploadAppVersionMethod,
        uploadAppVersionIsForce,
        uploadAppVersionCancel,
        uploadAppVersionSave,
        uploadAppVersionInfo,
        uploadAppVersionNumber,
        uploadAppVersionFileUrl,
        uploadAppVersionWgtUrl,

        appVersionListAO,
        select,
        selectLoading,
        pageInfo,

        deleteAppVersion,

        willUpdateAppVersion,
        updateAppVersion,
        willUpdateAppVersionDialog,
        updateAppVersionCancel,
        updateAppVersionSave,

        showQrCodeDialog,
        showQrCode,
        showQrCodeVersion,

        releaseAppVersion,
        callbackAppVersion,
        selectNowVersionVO,
        selectNowVersionLoading,

        isCollapse,
    }
}
